package com.remiss.seckilldemo.exception;

import com.remiss.seckilldemo.vo.ResposeBean;
import com.remiss.seckilldemo.vo.ResposeBeanEnum;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;



@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(Exception.class)
    public ResposeBean ExceptionHandler(Exception e){
        if(e instanceof GlobalExecption){
            GlobalExecption ex =(GlobalExecption) e;
            return ResposeBean.error(ex.getResposeBeanEnum());
        }else if(e instanceof BindException){
            BindException ex = (BindException)e;
            ResposeBean resposeBean = ResposeBean.error(ResposeBeanEnum.BIND_ERROR);
            resposeBean.setMessage("参数校验异常: "+ex.getBindingResult().getAllErrors().get(0).getDefaultMessage());
            return resposeBean;
        }
        return ResposeBean.error(ResposeBeanEnum.ERROR);
    }
}
